package com.ruoyi.asset.controller;

import com.ruoyi.asset.domain.AssetFlow;
import com.ruoyi.asset.domain.AssetInfo;
import com.ruoyi.asset.service.IAssetFlowService;
import com.ruoyi.asset.service.IAssetInfoService;
import com.ruoyi.asset.utils.PrintUtil;
import com.ruoyi.asset.utils.QrUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 资产信息Controller
 * 
 * @author yepanpan
 * @date 2024-12-20
 */
@RestController
@RequestMapping("/asset/qr")
public class AssetQrController extends BaseController
{
    @Autowired
    private IAssetInfoService assetInfoService;
    @Autowired
    private IAssetFlowService assetFlowService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 打印二维码
     */
    @GetMapping(value = "/print/{id}")
    public String printQr(@PathVariable("id") Long id, HttpServletRequest request)
    {
        AssetInfo assetInfo = assetInfoService.selectAssetInfoById(id);
        if(assetInfo == null){
            return "资产不存在";
        }

        String url = request.getRequestURL().toString().replace("print", "img");

        String configSize = configService.selectConfigByKey("qr.size");
        Integer qrSize = StringUtils.isEmpty(configSize) ? 300 : Integer.parseInt(configSize);
        configSize = configService.selectConfigByKey("qr.print");
        Integer printSize = StringUtils.isEmpty(configSize) ? 300 : Integer.parseInt(configSize);

        Map<String, Object> data = new HashMap<>();
        data.put("qrSize", qrSize);
        data.put("printSize", printSize);
        data.put("qrUrl", url);
        data.put("num", assetInfo.getNum());

        return PrintUtil.render("asset", data);

    }

    /**
     * 生成资产二维码
     */
    @GetMapping(value = "/img/{id}")
    public void getImg(@PathVariable("id") Long id, HttpServletResponse response)
    {
        AssetInfo assetInfo = assetInfoService.selectAssetInfoById(id);
        if(assetInfo == null){
            throw new ServiceException("非法请求");
        }

        String configSize = configService.selectConfigByKey("qr.size");
        Integer qrSize = StringUtils.isEmpty(configSize) ? 300 : Integer.parseInt(configSize);

        //创建或清空目录
        String path = RuoYiConfig.getProfile()+"/qr/";
        File qrFolder = new File(path);
        if(!qrFolder.exists()){
            qrFolder.mkdirs();
        }

        String qrPath = path + assetInfo.getId()+".png";
        File qrFile = new File(qrPath);
        if(!qrFile.exists()){
            QrUtil.genQr(assetInfo.getNum(), qrPath, qrSize);
        }

        if(!qrFile.exists()){
            throw new ServiceException("生成二维码失败");
        }

        QrUtil.writeQr(qrPath, response);
    }


    /**
     * 取二维码信息
     */
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam("num") String num)
    {
        AssetInfo assetInfo = assetInfoService.selectAssetInfoByNum(num);
        if (assetInfo == null) {
            return AjaxResult.error("资产不存在");
        }
        return AjaxResult.success(assetInfo);
    }
}
